package net.ryian.mail;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Properties;

import javax.mail.internet.InternetAddress;

import freemarker.template.utility.StringUtil;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class MailSender {
    public MailSender() {
    }

    private static HtmlEmail getConnectMail() throws Exception {
        HtmlEmail memail = new HtmlEmail();
        Properties prop = new Properties();
        InputStream is = null;

        try {
            is = MailSender.class.getClassLoader().getResourceAsStream("mail/email.properties");
            prop.load(is);
        } catch (Exception var4) {
            throw new Exception("mail/email.properties文件未找到！");
        }

        memail.setHostName(prop.getProperty("Host"));
        memail.setFrom(prop.getProperty("From"));
        memail.setAuthentication(prop.getProperty("userName"), prop.getProperty("password"));
        String isSsl = prop.getProperty("smtp.ssl");
        if(isSsl != null && "true".equalsIgnoreCase(isSsl)) {
            memail.setSSLOnConnect(Boolean.parseBoolean(isSsl));
        }
        is.close();
        return memail;
    }

    private static String getModel(Mail mail) {
        String content = null;

        try {
            Configuration e = new Configuration();

            Template template;
            try {
                e.setDefaultEncoding("UTF-8");
                e.setEncoding(Locale.getDefault(), "UTF-8");
                e.setClassForTemplateLoading(MailSender.class, "\\mail\\template");
                template = e.getTemplate(mail.getModel());
            } catch (Exception var6) {
                throw new Exception("模板必须放在\\mail\\template下");
            }


            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            OutputStreamWriter out = new OutputStreamWriter(bos,"UTF-8");
            template.process(mail.getMap(), out);
            byte[] lens = bos.toByteArray();
            content = new String(lens,"UTF-8");
            out.close();
        } catch (Exception var7) {
            var7.printStackTrace();
        }

        return content;
    }

    public static String sendMail(Mail mail) throws Exception {
        String mes = null;

        try {
            HtmlEmail e = getConnectMail();
            ArrayList attachment;
            int i;
            if(mail.getAddTo().size() != 0 || !mail.getAddTo().isEmpty()) {
                attachment = new ArrayList();

                for(i = 0; i < mail.getAddTo().size(); ++i) {
                    attachment.add(new InternetAddress((String)mail.getAddTo().get(i)));
                }

                e.setTo(attachment);
            }

            if(mail.getCc().size() != 0 || !mail.getCc().isEmpty()) {
                attachment = new ArrayList();

                for(i = 0; i < mail.getCc().size(); ++i) {
                    attachment.add(new InternetAddress((String)mail.getCc().get(i)));
                }

                e.setCc(attachment);
            }

            if(mail.getBcc().size() != 0 || !mail.getBcc().isEmpty()) {
                attachment = new ArrayList();

                for(i = 0; i < mail.getBcc().size(); ++i) {
                    attachment.add(new InternetAddress((String)mail.getBcc().get(i)));
                }

                e.setBcc(attachment);
            }

            e.setSubject(mail.getSubject());
            if(mail.getModel() != null && !"".equals(mail.getModel())) {
                mail.setMsg(getModel(mail));
            } else if(mail.getMsg() == null || "".equals(mail.getMsg())) {
                mail.setMsg(" ");
            }

            e.setCharset("utf-8");
            e.setHtmlMsg(mail.getMsg());
            if(mail.getAttachmentPath() != null && !"".equals(mail.getAttachmentPath())) {
                EmailAttachment var6 = new EmailAttachment();
                var6.setPath(mail.getAttachmentPath());
                var6.setDisposition("attachment");
                e.attach(var6);
            }

            mes = e.send();
            return mes;
        } catch (EmailException var5) {
            var5.printStackTrace();
            return null;
        }
    }
}
